#include <HAL/functionFactory.h>
#include <HAL/hal.h>
#include <utitl/logger.h>

#include <dlfcn.h>

#include <iostream>

using namespace ECG::Utitl;

namespace ECG
{
    namespace HAL
    {

        FuncFactoryPtr FuncFactory::m_funcFactory;

        FuncFactoryPtr FuncFactory::instance()
        {
            if (!m_funcFactory.get())
            {
                m_funcFactory.reset(new FuncFactory());
            }
            return m_funcFactory;
        }

        FuncInterfacePtr FuncFactory::getFuncByID(std::string id)
        {
            FuncInterfacePtr funcPtr;
            path += "lib"+id+".so";
            //path += id;
            //path += ".so";
            log_debug() << "lib path = " << path;
            void *handle = dlopen(path.c_str(), RTLD_LAZY);
            if(handle == NULL)
            {
                log_error() << "unable open file:" << path;
                return funcPtr;
            }
            FuncInterfacePtr (*createClass)() = (FuncInterfacePtr(*)())dlsym(handle, "createClass");
            if (NULL == createClass)
            {
                log_error() << "unable find method: createClass";
                return funcPtr;
            }
           funcPtr = createClass();
           return funcPtr;
        }

        FuncFactory::~FuncFactory()
        {

        }

        FuncFactory::FuncFactory() : path("/opt/ECG/lib/func/")
        {
            
        }

    }
}